﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BPM.Entity;
using Webdiyer.WebControls.Mvc;
using System.Data.Entity;

namespace BPM.Service.Services
{
    public class PendingPurchaseOrderListService : ServiceBase<PurchaseOrder>
    {
        /// <summary>
        /// 分页获取待验证的采购单
        /// </summary>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <returns></returns>
        public override PagedList<PurchaseOrder> GetAll(int pageIndex, int pageSize)
        {
            var q = this.Query();
            var result = q.Where(p => p.IsApproved == false).ToPagedList(pageIndex, pageSize);
            return EnableLazyLoading(result);
        }

        /// <summary>
        /// 根据当前采购员 查询他创建的采购单
        /// </summary>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="user"></param>
        /// <returns></returns>
        public PagedList<PurchaseOrder> GetAllByBuyer(int pageIndex, int pageSize, User user)
        {
            var q = this.Query();
            var result = q.Where(p => p.CreatorId == user.Id)
                .OrderBy(p => p.Id).ToPagedList(pageIndex, pageSize);
            return EnableLazyLoading(result);
        }


        protected override IQueryable<PurchaseOrder> PrepareToQuery(DbSet<PurchaseOrder> set)
        {
            return base.PrepareToQuery(set)
                .Include(p => p.ItemList)
                .Include(p => p.Barcode);
                //.Include(p => p.ItemList.AsQueryable().Include(i => i.MaterialDefinition));
        }
    }
}
